package servlet;

import com.google.gson.Gson;
import dao.Impl.StaffsDaoImpl;
import dao.StaffsDao;
import entitl.Staffs;
import vo.ResponseData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author czl
 * @Date 2024-03-07
 */
@WebServlet("/staffs_update")
public class staffsUpdate extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String phone = req.getParameter("phone");
        String pos = req.getParameter("pos");
//        Date update_time=new Date();
//        try {
//            update_time  = new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("update_time"));
//        } catch (ParseException e) {
//            throw new RuntimeException(e);
//        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime updateTime = LocalDateTime.parse("update_time");


        int id = Integer.parseInt(req.getParameter("id"));

//        Date add_time=new Date();
//        try {
//            add_time =new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("add_time"));
//        } catch (ParseException e) {
//            throw new RuntimeException(e);
//        }

        Staffs staffs= new Staffs();
        staffs.setName(name);
        staffs.setAge(age);
        staffs.setPhone(phone);
        staffs.setPos(pos);
        staffs.setUpdateTime(updateTime);
        staffs.setId(id);
//        staffs.setAdd_time(add_time);
//        staffs.setUpdate_time(update_time);

        StaffsDao dao = new StaffsDaoImpl();
        int row=dao.update(staffs);

        ResponseData responseData = new ResponseData();
        responseData.setMsg("修改成功");

        if(row!=1) {
            responseData.setMsg("修改失败");
            responseData.setCode(500);
        }

        String json = new Gson().toJson(responseData);
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);

        out.flush();
        out.close();
    }
}
